package com.ykdl.member.kid.set;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import oauth.signpost.http.HttpParameters;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.R.string;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ToggleButton;

import cn.jpush.android.api.JPushInterface;

import com.ykdl.member.kid.R;
import com.ykdl.member.base.GlobalContext;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.GroupsBean;
import com.ykdl.member.kid.beans.PushSettingBean;
import com.ykdl.member.kid.beans.TopicBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.WelcomeActivity;
import com.ykdl.member.kid.prefs.MyPrefs;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class PushSettingActivity extends BaseScreen implements OnClickListener{
	private ToggleButton zhuanjiaketang_bt;
	private Boolean isedit = false;
	private int zhuanjiaketang_bt_status = 0;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setTopTitle("推送消息设置", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isedit) {
					setSatus();
				}else {
					PushSettingActivity.this.finish();
				}
				
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.pushsettingactivity_layout);
		inint();
	}
	public void inint() {
		zhuanjiaketang_bt = (ToggleButton) findViewById(R.id.zhuanjiaketang_bt);
		zhuanjiaketang_bt.setOnClickListener(this);
		zhuanjiaketang_bt.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					zhuanjiaketang_bt_status =1;
				}else {
					zhuanjiaketang_bt_status =0;
				}
				isedit = true ;
			}
		});
		getSatus();
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
	private void getSatus() {
		showProgress("正在加载中,请稍等...");
		String  pageurl = KidConfig.PUSH_SETTING;
		Map<String, String> map = new HashMap<String, String>();
		HttpBaseRequest request  =  Wxxr.getInstance().getRequest(pageurl, map,null);
		 TaskManager.startHttpRequest(request, new getgroupTag(),  PushSettingBean.class);
	}
	class getgroupTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof PushSettingBean) {
				PushSettingBean bean = (PushSettingBean)t;
				if (bean.getHsh_notices()==1) {
					zhuanjiaketang_bt.setChecked(true);
				}else {
					zhuanjiaketang_bt.setChecked(false);
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	private void setSatus() {
		showProgress("正在加载中,请稍等...");
		String  pageurl = KidConfig.PUSH_SETTING;
		HttpParameters param = new HttpParameters();
		param.put("hsh_notices", zhuanjiaketang_bt_status+"");
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl, param, "");
		 TaskManager.startHttpRequest(request, new setgroupTag(),  PushSettingBean.class);
	}
	class setgroupTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof PushSettingBean) {
				PushSettingBean bean = (PushSettingBean)t;
				if (bean.getError()==null) {
					if (bean.getHsh_notices()==0) {
						JPushInterface.stopPush(PushSettingActivity.this);
					}else {
						JPushInterface.resumePush(PushSettingActivity.this);
					}
				}
				PushSettingActivity.this.finish();
				}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if (isedit) {
			setSatus();
		}else {
			PushSettingActivity.this.finish();
		}
		super.onBackPressed();
	}
}
